package com.human.server.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.human.server.entity.Positions;
import com.human.server.model.R;
import com.human.server.model.request.PositionsRequest;
import com.human.server.service.impl.PositionsServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/human/positions")
public class PositionsController {

    @Autowired
    private PositionsServiceImpl positionsService;

    @RequestMapping("/page")
    public R page(@RequestBody PositionsRequest request) {
        return R.success(positionsService.page(request, new LambdaQueryWrapper<Positions>()
                .eq(StrUtil.isNotEmpty(request.getName()), Positions::getName, request.getName())
                .eq(request.getDepartmentId() != null, Positions::getDepartmentId, request.getDepartmentId())
        ));
    }

    /**
     * 新增
     * @param positions
     * @return
     */
    @RequestMapping("/add")
    public R add(@RequestBody Positions positions) {
        return R.success(positionsService.save(positions));
    }

    /**
     * 更新
     * @param positions
     * @return
     */
    @RequestMapping("/update")
    public R update(@RequestBody Positions positions) {
        return R.success(positionsService.updateById(positions));
    }

    /**
     * 删除
     * @param positions
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Positions positions) {
        return R.success(positionsService.removeById(positions.getId()));
    }

    /**
     * 查询
     * @param positions
     * @return
     */
    @RequestMapping("/query")
    public R query(@RequestBody Positions positions) {
        return R.success(positionsService.getById(positions.getId()));
    }

    /**
     * 列表
     * @return
     */
    @RequestMapping("/list")
    public R list() {
        return R.success(positionsService.list());
    }

}
